package com.camerareader.util;

import android.content.Context;
import android.content.SharedPreferences;


/**
 * Created by shi on 16/8/29.
 */
public class SharedPreferencesUtil {

    private static SharedPreferences sp;

    private static String spName = "spName";


    private static Context mContext;

    public static void init(Context context){
        mContext=context;
    }

    private static SharedPreferences getDefaultSharedPreferences() {
        return getSharedPreferences(spName, Context.MODE_PRIVATE);
    }

    public static SharedPreferences getSharedPreferences(String name, int mode) {
        return mContext.getSharedPreferences(name, mode);
    }

    public static void put(String key, Object obj) {
        SharedPreferences sp = getDefaultSharedPreferences();
        SharedPreferences.Editor edit = sp.edit();

        if (obj == null) {

        } else if (obj instanceof Boolean) {
            edit.putBoolean(key, (Boolean) obj);
        } else if (obj instanceof Float) {
            edit.putFloat(key, (Float) obj);
        } else if (obj instanceof Long) {
            edit.putLong(key, (Long) obj);
        } else if (obj instanceof Integer) {
            edit.putInt(key, (Integer) obj);
        } else if (obj instanceof String) {
            edit.putString(key, obj.toString());
        }
        edit.commit();
    }




    public static int get(String s, int defaultValue) {
        return getDefaultSharedPreferences().getInt(s, defaultValue);
    }

    public static float get(String s, float defaultValue) {
        return getDefaultSharedPreferences().getFloat(s, defaultValue);
    }

    public static long get(String s, long defaultValue) {
        return getDefaultSharedPreferences().getLong(s, defaultValue);
    }

    public static String get(String s, String defaultValue) {
        return getDefaultSharedPreferences().getString(s, defaultValue);
    }

    public static boolean get(String s, boolean defaultValue) {
        return getDefaultSharedPreferences().getBoolean(s, defaultValue);
    }


    public static void clear() {//清楚所有数据
        SharedPreferences sp = getDefaultSharedPreferences();
        SharedPreferences.Editor edit = sp.edit();
        edit.clear();
        edit.commit();
    }

}
